import { createStore } from "redux";
// 创建全局的状态管理部分
const defState: IRootState = {
  toastData: {
    show: false,
    msg: "测试的提示",
    type: "success",
  },
};

// reducer
const reducer = (state: IRootState = defState, action: IAction) => {
  state = JSON.parse(JSON.stringify(state));

  // 提示方法对外可以操作state的数据
  switch (action.type) {
    case "SHOW_TOAST":
      // 修改 toastData的数据
      state.toastData.type = action.payload.type;
      state.toastData.msg = action.payload.msg;
      state.toastData.show = true;
      break;
    case "HIDE_TOAST":
      state.toastData.show = false;
      break;
  }

  return state;
};

const store = createStore<IRootState, IAction, any, any>(reducer);

export default store;
